package cn.wolfcode.p2p.bussness.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.bussness.domain.AccountFlow;
import cn.wolfcode.p2p.bussness.domain.SystemAccount;
import cn.wolfcode.p2p.bussness.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.bussness.service.IAccountFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {
    @Autowired
    private AccountFlowMapper accountFlowMapper;

    @Override
    public int save(AccountFlow accountFlow) {
        return accountFlowMapper.insert(accountFlow);
    }

    /*线下充值流水*/
    @Override
    public void createRechargeOfflineFlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE, "线下创智成功" + amount + "元");
    }

    @Override
    public void createCidFreecedGlow(Account account, BigDecimal amount) {
        createFlow(account, amount, Constants.ACCOUNT_ACTIONTYPE_BID_FREEZED, "投标冻结" + amount + "元");
    }

    public void createFlow(Account account, BigDecimal amount, int actionType, String nore) {
        AccountFlow flow = new AccountFlow();
        flow.setAccountId(account.getId());//设置用户的账户id
        flow.setActionTime(new Date());//设置流水时间
        flow.setAmount(amount);//记录流水金额
        flow.setActionType(actionType);//记录流水类型
        flow.setUsableAmount(account.getUsableAmount());//记录变化后的可用金额
        flow.setFreezedAmount(account.getFreezedAmount());//设置变化后的冻结金额
        flow.setNote(nore);//设置备注消息
        this.save(flow);
    }

    @Override
    public void createVBidFaileFlow(Account account, BigDecimal amount) {
		createFlow(account,amount,Constants.ACCOUNT_ACTIONTYPE_BID_UNFREEZED,"投标水白,取消冻结:"+amount+"元");
    }

    @Override
    public void createRechargeMarageFlow(Account account, BigDecimal amount) {
        createFlow(account,amount,Constants.ACCOUNT_ACTIONTYPE_CHARGE,"支付平台借款手续费:"+amount+"元");
    }



    @Override
    public void createAccountManagemrntChargeFlow(Account account, BigDecimal amount) {
        createFlow(account,amount,Constants.ACCOUNT_ACTIONTYPE_CHARGE,"支付平台收取借款手续费:"+amount+"元");
    }

    @Override
    public void createBorrowSuressFlow(Account account, BigDecimal amount) {
        createFlow(account,amount,Constants.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL,"借款成功:"+amount+"元");
    }

    @Override
    public void createVBidSuccessFlow(Account account, BigDecimal amount) {
        createFlow(account,amount,Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL,"投标成功:"+amount+"元");
    }

    @Override
    public void creagainReturnMoneyFlow(Account account, BigDecimal amount) {
        createFlow(account,amount,Constants.SYSTEM_ACCOUNT_ACTIONTYPE_MANAGE_CHARGE,"回款:"+amount+"元");
    }


}
